<template>
  <FullscreenCard class="wrapper">
		<div class="grid" ref="charDom"></div>
	</FullscreenCard>
</template>

<script setup lang="ts">
import * as echarts from 'echarts'
import FullscreenCard from '@/components/FullscreenCard'
import { styleType } from 'element-plus/es/components/table-v2/src/common'

const charDom = ref<any>(null)

const dataList = [{
  id: '1',
  name: '青铜器',
  value: 62
},{
  id: '2',
  name: '瓷器',
  value: 232
},{
  id: '3',
  name: '陶器',
  value: 162
},{
  id: '4',
  name: '书画',
  value: 102
},{
  id: '5',
  name: '绘画',
  value: 261
},{
  id: '6',
  name: '玉石器',
  value: 49
},{
  id: '7',
  name: '石刻',
  value: 49
}]
    

const initChart = () => {
  const myChart = echarts.init(charDom.value)
  const dataValues = dataList.map((v:any) => v.value)
  // 计算最大值
  const maxValue = Math.max(...dataValues)
  // 设置圆柱体高度
  const roundedMaxValue = Math.ceil(maxValue/100)*100
  // 背景柱状图的数据
  const maxData = new Array(dataList.length).fill(roundedMaxValue)

  const labelSetting = {
    show: true,
    position: 'top',
    offset: [0, -10],
    formatter: (param:any) => {
      return ((param.value / roundedMaxValue) * 100).toFixed(0) + '%'
    },
    fontSize: 18,
    fontFamily: 'Arial'
  }
  const option =  {
    // 移动柱状图
    type: 'move',
    // 移动数量
    moveNum: 6,
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow'
      },
      formatter: (params:any[]) => {
        const param = params.filter(item => item.seriesName === '内部柱子')[0]
        if(param) {
          return `${param.axisValueLabel}:${param.value}`
        }
      }
    },
    grid: {
      left: 60,
      right: 60,
      top: 50,
      bottom: 40
    },
    xAxis: {
      type: 'category',
      boundaryGap: false,
      // 分割线
      splitLine: { show: false },
      // 坐标轴线
      axisLine: { show: false },
      // 刻度线
      axisTick: { show: false },
      splitArea: {
        interval: 0,
        show: true
      },
      data: dataList.map(v => ({
        value: v.name,
        textStyle: {
          align: 'center'
        }
      }))
    },
    yAxis: {
      type: 'value',
      nameTextStyle: {
        color: '#ffffff',
        fontSize: 22,
      },
      // 分割线
      splitLine: { 
        show: true,
        lineStyle: {
          color: '#ffffff',
          type: 'dashed' 
        }
      },
      // 坐标轴线
      axisLine: { show: false },
      // 刻度线
      axisTick: { show: false },
      axisLabel: {
        show: true,
        textStyle: {
          color: '#ffffff',
          fontSize: 21,
        },
      }
    },
    series: [
      {
        name: '上部圆',
        type: 'pictorialBar',
        silent: true, // 不显示提示消息
        symbolSize: [55,10],
        symbolOffset: [0,-6],
        symbolPosition: 'end',
        z: 12,
        color: '#ffe7da',
        data: maxData
      },
      {
        name: '背景圆柱',
        type: 'bar',
        barWidth: 55,
        barGap: '10%',
        itemStyle: {
          normal: {
          //   color: new echarts.graphic.LinearGradient(0,0,1,1, [
          //     {
          //       offset: 0,
          //       color: 'rgba(84,165,255,0.31)'
          //     },
          //     {
          //       offset: 0.55,
          //       color: 'rgba(0,115,255,0.14)'
          //     },
          //     {
          //       offset: 1,
          //       color: 'rgba(4,4,26,0)'
          //     }
          //   ])
            color: '#fff1e9',
          },
          // opacity: 0.8
        },
        data: maxData
      },
      {
        name: '内部柱子',
        type: 'bar',
        barWidth: 55,
        z: 12,
        barGap: '-100%',
        itemStyle: {
          // color: (params: any) => {
          //   return new echarts.graphic.LinearGradient(0,0,0,1, [
          //     {
          //       offset: 0,
          //       color: '#0E60B2'
          //     },
          //     {
          //       offset: 1,
          //       color: '#1FAEDE'
          //     }
          //   ], false)
          // },
          // opacity: 0.7
          color: '#ffb487',
        },
        data: dataValues
      },
      {
        name: '内部柱子上部圆',
        type: 'pictorialBar',
        silent: true, // 不显示提示消息
        symbolSize: [55,10],
        symbolOffset: [0,-6],
        symbolPosition: 'end',
        z: 16,
        color: '#ff8c45',
        label: labelSetting,
        data: dataValues
      },
      {
        name: '内部柱子底部圆',
        type: 'pictorialBar',
        silent: true, // 不显示提示消息
        symbolSize: [55,10],
        symbolOffset: [0,6],
        z: 12,
        color: '#ffb487',
        data: dataValues
      },
    ],
  }

  myChart.setOption(option)

  window.addEventListener('resize', () => {
    myChart.resize()
  })
}
onMounted(() => {
  initChart()
})
</script>

<style lang="scss" scoped>
.wrapper {
  padding: 20px;
	background-color: var(--vt-c-white);
}
.grid {
  box-sizing: border-box;
  width: 100%;
  height: 100%;
}
.grid-popover {
  position: absolute; 
  right: 200px; 
  top: 60px;
  z-index: 3000;
  background-color: #fff;
  min-width: 150px;
  border-radius: 4px;
  border: 1px solid #EBEEF5;
  padding: 12px;
}
</style>
